Ext.ns("smallType");
smallType.init = function() {
	var isRight_smallType = false;// 表单填写是否正确
	var isEdit_smallType = false;// 是否进行修改
	smallType.big_Id;// 定义选定的大类型的序列号
	smallType.big_Name;//定义选定的大类型名称
	var sm_smallType = new Ext.grid.CheckboxSelectionModel({
				handleMouseDown : Ext.emptyFn
			});
	var cm_smallType = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			sm_smallType, {
				header : '小商品类型',
				dataIndex : 'small_Name',
				sortable : true
			}, {
				header : '创建时间',
				dataIndex : 'small_Date',
				sortable : true,
				renderer : Ext.util.Format.dateRenderer('Y-m-d')
			}, {
				header : '状态',
				dataIndex : 'small_State',
				sortable : true
			}]);

	var store_smallType = new Ext.data.JsonStore({
				url : 'showSmallType.action',
				totalProperty : 'total',
				root : 'smalltypes',
				fields : [{
							name : "small_Name",
							mapping : 'small_Name',
							type : 'string'
						}, {
							name : "small_Date",
							mapping : 'small_Date',
							type : 'date'
						}, {
							name : "small_State",
							mapping : 'small_State',
							type : 'string'
						}]
			});// 定义点击大类型后加载相应的小类型
	var store_smallSearchBigTypes = new Ext.data.JsonStore({
				url : 'showBigType.action',
				totalProperty : 'total',
				root : 'bigtypes',
				fields : [{
							name : "big_Id",
							type : 'int'
						}, {
							name : "big_Name",
							type : 'string'
						}]
			});
	var smallTypePanel = new Ext.grid.GridPanel({
		deferredRender : true,
		sm : sm_smallType,
		cm : cm_smallType,
		store : store_smallType,
		loadMask : true,
		height : 400,
		columnWidth : .5,
		viewConfig : {
			columnsText : '选择显示列',
			forceFit : true
		},
		tbar : new Ext.Toolbar(['-', {
			text : '删除选中项',
			handler : function() {
				var rows = smallTypePanel.getSelectionModel().getSelections();
				if (rows.length < 1) {
					Ext.Msg.alert("提示", "请选择一个或多个选项！");
					return false;
				} else {
					var small_Names = []; // 要删除的id
					Ext.each(rows, function(item) {
								big_Names.push(item.data.small_Name);
							});
					Ext.Ajax.request({
								url : 'deleteBigType',
								method : 'post',
								params : {
									small_Name : small_Names
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									if (obj.success == false) {
										Ext.Msg.alert("提示", "删除失败");
									} else {
										Ext.Msg.alert("提示", "删除成功");
										smallTypePanel.view.refresh();
									}
								},
								failure : function(response) {
								}
							});

				}
			}
		}, '-', {
			text : '刷新页面',
			handler : function() {
				Ext.getCmp("pagetool_smallType").doRefresh();
			}
		}]),
		bbar : new Ext.PagingToolbar({
					id : 'pagetool_smallType',
					pageSize : 10,
					store : store_smallType,
					displayInfo : true,
					displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg : "没有记录"
				}),
		listeners : {
			'rowdblclick' : function(grid, rowindex, event) {
				var record = grid.getStore().getAt(rowindex);
				editPanel_smallType.getForm().loadRecord(record);
				Ext.getCmp("small_Name").getEl().dom.readOnly = true;
				isEdit_smallType = true;
			}
		}
	});
	var editPanel_smallType = new Ext.FormPanel({
		defaultType : 'textfield',
		height : 400,
		columnWidth : .5,
		labelAlign : true,
		msgTarget : 'side',
		bodyStyle : {
			marginLeft : '100px',
			padding : '100px'
		},
		viewConfig : {
			forceFit : true
		},
		items : [{
					id : 'small_BigName',
					fieldLabel : '大类型名',
					allowBlank : false,
					blankText : '对不起，请选择状态栏！',
					emptyText : '请选择',
					xtype : 'combo',
					mode : 'remote',
					triggerAction : 'all',
					valueField : 'big_Name',
					displayField : 'big_Name',
					pageSize : 8,
					minListWidth : 300,
					store : store_smallSearchBigTypes
				}, {
					id : 'small_Name',
					fieldLabel : '小类型名',
					allowBlank : false,
					blankText : '对不起，类型名不能为空',
					minLength : 2,
					minLengthText : '请输入类型名大于两个汉字',
					maxLength : 10,
					maxLengthText : '请输入类型名小于十个汉字',
					name : 'smallType.small_Name',
					listeners : {
						'blur' : function() {
							if (Ext.getCmp('small_Name').getValue() != ''
									&& isEdit_smallType != true) {
								Ext.Ajax.request({
											url : 'ValidSmallType.action',
											method : 'post',
											params : {
												bigType : Ext
														.getCmp('small_Name')
														.getValue()
											},
											success : function(response) {
												var obj = Ext
														.decode(response.responseText);
												if (obj.success == false) {
													Ext
															.getCmp('small_Name')
															.markInvalid('类型名已被使用');
												} else {
													isRight_smallType = true;
												}
											},
											failure : function(response) {
												Ext.getCmp('small_Name')
														.markInvalid('服务器失败');
											}

										});
							}
						}
					}
				}, {
					id : 'small_Date',
					fieldLabel : '创建日期',
					xtype : 'datefield',
					emptyText : '请选择',
					format : 'Y-m-d',
					allowBlank : false,
					blankText : '对不起，创建日期不能为空',
					name : 'smallType.small_Date'
				}, {
					id : 'small_State',
					fieldLabel : '状态',
					allowBlank : false,
					blankText : '对不起，请选择状态栏！',
					emptyText : '请选择',
					xtype : 'combo',
					mode : 'local',
					triggerAction : 'all',
					valueField : 'text',
					displayField : 'value',
					name : 'smallType.small_State',
					store : new Ext.data.SimpleStore({
								fields : ['value', 'text'],
								data : [['激活', 1], ['封锁', 0]]
							})
				}],
		buttonAlign : 'center',
		buttons : [{
			text : '提交',
			handler : function() {
				if (editPanel_smallType.getForm().isValid()
						&& isRight_smallType == true) {
					editPanel_smallType.getForm().submit({
								url : 'addSmallType',
								method : 'post',
								params : {
									bigType_Id : smallType.big_Id
								},// 有问题
								success : function(form, action) {
									Ext.Msg.alert('提示', "增加成功");
									editPanel_smallType.getForm().reset();
									isRight_smallType = false;
									store_smallType.load({
												params : {
													big_Name :smallType.big_Name,
													start : 0,
													limit : 12
												}
											});
								},
								failure : function(form, action) {
									Ext.Msg.alert('提示', "增加失败！！！！");
								}
							});
				}
			}
		}, {
			text : '修改',
			handler : function() {
				isRight_smallType = true;
				if (editPanel_smallType.getForm().isValid()
						&& isRight_smallType == true
						&& isEdit_smallType == true) {
					editPanel_smallType.getForm().submit({
								url : 'editSmallType',
								method : 'post',
								success : function(form, action) {
									Ext.Msg.alert('提示', "修改成功");
									editPanel_smallType.getForm().reset();
									Ext.getCmp("pagetool_smallType")
											.doRefresh();
									isRight_smallType = false;
								},
								failure : function(form, action) {
									Ext.Msg.alert('提示', "修改失败！！！！");
								}
							});
				}
			}
		}, {
			text : '重置',
			handler : function() {
				editPanel_smallType.getForm().reset();
				Ext.getCmp("small_Name").getEl().dom.readOnly = false;
				isRight_smallType = false;
				isEdit_smallType = false;
			}

		}]
	});
	var smallTypeMain = new Ext.Panel({
				title : '商品小类型管理',
				id : 'smallTypePanel',
				frame : true,
				deferredRender : true,
				closeAction : 'hide',
				cm : cm_smallType,
				store : store_smallType,
				loadMask : true,
				autoWidth : true,
				autoHeight : true,
				closable : true,
				layout : 'column',
				layoutConfig : {
					columns : 2,
					rows : 1
				},
				viewConfig : {
					forceFit : true
				},
				items : [smallTypePanel, editPanel_smallType]
			});
	Ext.getCmp("small_BigName").on('select', function(combo) {
		smallType.big_Name= combo.getValue();
		var index = store_smallSearchBigTypes
				.find('big_Name', combo.getValue());
		smallType.big_Id = store_smallSearchBigTypes.getAt(index).get("big_Id");
		store_smallType.load({
					params : {
						big_Name : combo.getValue(),
						start : 0,
						limit : 12
					}
				});
	});
}
Ext.onReady(smallType.init);